﻿using Jugg.Admin.Framework.Controllers;
using Jugg.Admin.Framework.Menu;
using Jugg.Mapping.Setting;
using Jugg.Services.Setting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Jugg.Core.MediaItem;

namespace Jugg.Admin.Controllers
{
    [RoutePrefix("setting")]
    public class SettingController : BaseAdminController
    {
        // GET: Setting
        private ISettingService _settingService;

        public SettingController(ISettingService settingService)
        {
            this._settingService = settingService;
        }

        /// <summary>
        /// 基数设置
        /// </summary>
        /// <returns></returns>
        [Route("", Name = "settingIndex")]
        [Descriper("基数设置", true, "menu-icon fa fa-caret-right", FatherResource = "Jugg.Admin.Controllers.SystemManageController")]
        public ActionResult SettingIndex()
        {
            return View(_settingService.getAllCache());
        }

        /// <summary>
        /// 保存设置数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        //[HttpGet]
        [Route("saveSetting", Name = "dataSetting")]
        [Descriper("保存数据设置", false, FatherResource = "Jugg.Admin.Controllers.SystemManageController.SystemIndex")]
        public JsonResult SaveDataSetting(string name, string value)
        {
            if(String.IsNullOrEmpty(name))
            {
                AjaxData.Message = "错误操作";
                return Json(AjaxData, JsonRequestBehavior.AllowGet);
            }
            if(String.IsNullOrEmpty(value))
            {
                AjaxData.Message = "请输入";
                return Json(AjaxData, JsonRequestBehavior.AllowGet);
            }
            switch (name)
            {
                case SettingKey.marketer: 
                case SettingKey.marketerUpLevel: 
                case SettingKey.marketerTwoLevel:
                    if (!value.isInteger())
                    {
                        AjaxData.Message = "请输入正整数";
                        return Json(AjaxData, JsonRequestBehavior.AllowGet);
                    }
                    break;
                case SettingKey.taxRate:
                    if (!value.isPercent())
                    {
                        AjaxData.Message = "请输入百分比";
                        return Json(AjaxData, JsonRequestBehavior.AllowGet);
                    }
                    break;
            }
            var reuslt = _settingService.saveSetting(name, value);
            AjaxData.Status = reuslt.Success;
            AjaxData.Message = reuslt.Message;
            return Json(AjaxData, JsonRequestBehavior.AllowGet);
        }


    }
}